﻿using System;
using System.Collections.Generic;
using System.Linq;
using MealBooking.Core.Models.Dishes;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Text;

namespace MealBooking.Core.Models.Meals
{
    public class Meal
    {
        public string Id { get; set; }
        public MealCategory Category { get; set; }
        public double Price
        {
            get
            {
                if (Category != null && Category.Price != 0)
                {
                    return Category.Price;
                }
                if (Dishes != null && Dishes.Count != 0)
                {
                    return Dishes.Sum(d => d.Price);
                }
                return 0;
            }
        }
        public List<Dish> Dishes { get; set; }

        public override bool Equals(object obj)
        {
            var meal = obj as Meal;
            if (!this.Category.Id.Equals(meal.Category.Id))
            {
                return false;
            }
            if (this.Dishes.Count != meal.Dishes.Count)
            {
                return false;
            }
            this.Dishes.OrderBy(o => o.Id);
            meal.Dishes.OrderBy(o => o.Id);
            int sum = this.Dishes.Count;
            for (int i = 0; i < sum; i++)
            {
                if (!this.Dishes.ElementAt(i).Id.Equals(meal.Dishes.ElementAt(i).Id))
                {
                    return false;
                }
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (Dishes != null && Dishes.Count != 0)
            {
                Dishes.ForEach(d => sb.Append(" ").Append(d.Name));
                sb.Append(" ");
            }
            return sb.ToString();
        }
    }

    public class MealWithSum
    {
        public Meal Meal { get; set; }
        public int MealSum { get; set; }        
    }
}